package com.hndct.util;

import com.hndct.config.Constant;

import java.security.MessageDigest;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	
	/**
	 * 判断字符串是否为空
	 */
	public static boolean isEmpty(String str){
		if(str == null || "".equals(str.trim())){
			return true;
		}
		return false;
	}
	
	/**
	 * 获取验证码
	 */
	public static String getMsgCode(){
		String res = "";
		
		for(int i = 0;i<Constant.msgCodeLeng;i++){
			res += RandomUtil.getRandomNum(10);
		}
		
		return res;
	}
	
	/**
	 * 将密码加密后返回
	 */
	public static String getMd5WithSalt(String str,String salt){
		return MD5(str+salt);
	}
	
	public static String MD5(String s) {
	    try {
	        MessageDigest md = MessageDigest.getInstance("MD5");
	        byte[] bytes = md.digest(s.getBytes("utf-8"));
	        return toHex(bytes);
	    }
	    catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}

	private static String toHex(byte[] bytes) {
	    final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
	    StringBuilder ret = new StringBuilder(bytes.length * 2);
	    for (int i=0; i<bytes.length; i++) {
	        ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
	        ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
	    }
	    return ret.toString();
	}

	public static String getOrderPrefix() {
		Calendar c = Calendar.getInstance();
		String year = c.get(Calendar.YEAR)+"";
		String month = (c.get(Calendar.MONTH)+1) > 9 ? (c.get(Calendar.MONTH)+1+"") : "0"+(c.get(Calendar.MONTH)+1);
		String day = c.get(Calendar.DATE) > 9 ? c.get(Calendar.DATE)+"" : "0"+c.get(Calendar.DATE);
		String hour = c.get(Calendar.HOUR_OF_DAY) > 9 ? c.get(Calendar.HOUR_OF_DAY)+"" : "0"+c.get(Calendar.HOUR_OF_DAY);
		String min = c.get(Calendar.MINUTE) > 9 ? c.get(Calendar.MINUTE)+"" : "0"+c.get(Calendar.MINUTE);
		String second = c.get(Calendar.SECOND) > 9 ? c.get(Calendar.SECOND)+"" : "0"+c.get(Calendar.SECOND);
		
		return year+month+day+hour+min+second;
	}


	/**
	 * 功能描述:
	 * @param email:
	 * @Return: boolean
	 * @Auther: YuZhenDong
	 * @Date: 2019/4/4
	 * @Discription: 验证邮箱
	 */
	public static boolean isEmail(String email){
		if (null==email || "".equals(email)){
			return false;
		}
		String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		Pattern p = Pattern.compile(regEx1);
		Matcher m = p.matcher(email);
		if(m.matches()){
			return true;
		}else{
			return false;
		}
	}
//	public static void main(String[] args) {
//		System.out.println(getOrderPrefix());
//	}
	
}
